﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// 读写网络的辅助类
	/// </summary>
	// Token: 0x02000143 RID: 323
	public class ReadWriteNetHelper
	{
		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Boolean,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C4E RID: 7246 RVA: 0x0008BF80 File Offset: 0x0008A180
		public static OperateResult<TimeSpan> Wait(IReadWriteNet readWriteNet, string address, bool waitValue, int readInterval, int waitTimeout)
		{
			DateTime now = DateTime.Now;
			OperateResult<bool> operateResult;
			for (;;)
			{
				operateResult = readWriteNet.ReadBool(address);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					break;
				}
				bool flag2 = operateResult.Content == waitValue;
				if (flag2)
				{
					goto Block_2;
				}
				bool flag3 = waitTimeout > 0 && (DateTime.Now - now).TotalMilliseconds > (double)waitTimeout;
				if (flag3)
				{
					goto Block_4;
				}
				Thread.Sleep(readInterval);
			}
			return OperateResult.CreateFailedResult<TimeSpan>(operateResult);
			Block_2:
			return OperateResult.CreateSuccessResult<TimeSpan>(DateTime.Now - now);
			Block_4:
			return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout.ToString());
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Int16,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C4F RID: 7247 RVA: 0x0008C034 File Offset: 0x0008A234
		public static OperateResult<TimeSpan> Wait(IReadWriteNet readWriteNet, string address, short waitValue, int readInterval, int waitTimeout)
		{
			DateTime now = DateTime.Now;
			OperateResult<short> operateResult;
			for (;;)
			{
				operateResult = readWriteNet.ReadInt16(address);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					break;
				}
				bool flag2 = operateResult.Content == waitValue;
				if (flag2)
				{
					goto Block_2;
				}
				bool flag3 = waitTimeout > 0 && (DateTime.Now - now).TotalMilliseconds > (double)waitTimeout;
				if (flag3)
				{
					goto Block_4;
				}
				Thread.Sleep(readInterval);
			}
			return OperateResult.CreateFailedResult<TimeSpan>(operateResult);
			Block_2:
			return OperateResult.CreateSuccessResult<TimeSpan>(DateTime.Now - now);
			Block_4:
			return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout.ToString());
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.UInt16,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C50 RID: 7248 RVA: 0x0008C0E8 File Offset: 0x0008A2E8
		public static OperateResult<TimeSpan> Wait(IReadWriteNet readWriteNet, string address, ushort waitValue, int readInterval, int waitTimeout)
		{
			DateTime now = DateTime.Now;
			OperateResult<ushort> operateResult;
			for (;;)
			{
				operateResult = readWriteNet.ReadUInt16(address);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					break;
				}
				bool flag2 = operateResult.Content == waitValue;
				if (flag2)
				{
					goto Block_2;
				}
				bool flag3 = waitTimeout > 0 && (DateTime.Now - now).TotalMilliseconds > (double)waitTimeout;
				if (flag3)
				{
					goto Block_4;
				}
				Thread.Sleep(readInterval);
			}
			return OperateResult.CreateFailedResult<TimeSpan>(operateResult);
			Block_2:
			return OperateResult.CreateSuccessResult<TimeSpan>(DateTime.Now - now);
			Block_4:
			return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout.ToString());
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Int32,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C51 RID: 7249 RVA: 0x0008C19C File Offset: 0x0008A39C
		public static OperateResult<TimeSpan> Wait(IReadWriteNet readWriteNet, string address, int waitValue, int readInterval, int waitTimeout)
		{
			DateTime now = DateTime.Now;
			OperateResult<int> operateResult;
			for (;;)
			{
				operateResult = readWriteNet.ReadInt32(address);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					break;
				}
				bool flag2 = operateResult.Content == waitValue;
				if (flag2)
				{
					goto Block_2;
				}
				bool flag3 = waitTimeout > 0 && (DateTime.Now - now).TotalMilliseconds > (double)waitTimeout;
				if (flag3)
				{
					goto Block_4;
				}
				Thread.Sleep(readInterval);
			}
			return OperateResult.CreateFailedResult<TimeSpan>(operateResult);
			Block_2:
			return OperateResult.CreateSuccessResult<TimeSpan>(DateTime.Now - now);
			Block_4:
			return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout.ToString());
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.UInt32,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C52 RID: 7250 RVA: 0x0008C250 File Offset: 0x0008A450
		public static OperateResult<TimeSpan> Wait(IReadWriteNet readWriteNet, string address, uint waitValue, int readInterval, int waitTimeout)
		{
			DateTime now = DateTime.Now;
			OperateResult<uint> operateResult;
			for (;;)
			{
				operateResult = readWriteNet.ReadUInt32(address);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					break;
				}
				bool flag2 = operateResult.Content == waitValue;
				if (flag2)
				{
					goto Block_2;
				}
				bool flag3 = waitTimeout > 0 && (DateTime.Now - now).TotalMilliseconds > (double)waitTimeout;
				if (flag3)
				{
					goto Block_4;
				}
				Thread.Sleep(readInterval);
			}
			return OperateResult.CreateFailedResult<TimeSpan>(operateResult);
			Block_2:
			return OperateResult.CreateSuccessResult<TimeSpan>(DateTime.Now - now);
			Block_4:
			return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout.ToString());
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Int64,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C53 RID: 7251 RVA: 0x0008C304 File Offset: 0x0008A504
		public static OperateResult<TimeSpan> Wait(IReadWriteNet readWriteNet, string address, long waitValue, int readInterval, int waitTimeout)
		{
			DateTime now = DateTime.Now;
			OperateResult<long> operateResult;
			for (;;)
			{
				operateResult = readWriteNet.ReadInt64(address);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					break;
				}
				bool flag2 = operateResult.Content == waitValue;
				if (flag2)
				{
					goto Block_2;
				}
				bool flag3 = waitTimeout > 0 && (DateTime.Now - now).TotalMilliseconds > (double)waitTimeout;
				if (flag3)
				{
					goto Block_4;
				}
				Thread.Sleep(readInterval);
			}
			return OperateResult.CreateFailedResult<TimeSpan>(operateResult);
			Block_2:
			return OperateResult.CreateSuccessResult<TimeSpan>(DateTime.Now - now);
			Block_4:
			return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout.ToString());
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.UInt64,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C54 RID: 7252 RVA: 0x0008C3B8 File Offset: 0x0008A5B8
		public static OperateResult<TimeSpan> Wait(IReadWriteNet readWriteNet, string address, ulong waitValue, int readInterval, int waitTimeout)
		{
			DateTime now = DateTime.Now;
			OperateResult<ulong> operateResult;
			for (;;)
			{
				operateResult = readWriteNet.ReadUInt64(address);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					break;
				}
				bool flag2 = operateResult.Content == waitValue;
				if (flag2)
				{
					goto Block_2;
				}
				bool flag3 = waitTimeout > 0 && (DateTime.Now - now).TotalMilliseconds > (double)waitTimeout;
				if (flag3)
				{
					goto Block_4;
				}
				Thread.Sleep(readInterval);
			}
			return OperateResult.CreateFailedResult<TimeSpan>(operateResult);
			Block_2:
			return OperateResult.CreateSuccessResult<TimeSpan>(DateTime.Now - now);
			Block_4:
			return new OperateResult<TimeSpan>(StringResources.Language.CheckDataTimeout + waitTimeout.ToString());
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Boolean,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C55 RID: 7253 RVA: 0x0008C46C File Offset: 0x0008A66C
		[DebuggerStepThrough]
		public static Task<OperateResult<TimeSpan>> WaitAsync(IReadWriteNet readWriteNet, string address, bool waitValue, int readInterval, int waitTimeout)
		{
			ReadWriteNetHelper.<WaitAsync>d__7 <WaitAsync>d__ = new ReadWriteNetHelper.<WaitAsync>d__7();
			<WaitAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
			<WaitAsync>d__.readWriteNet = readWriteNet;
			<WaitAsync>d__.address = address;
			<WaitAsync>d__.waitValue = waitValue;
			<WaitAsync>d__.readInterval = readInterval;
			<WaitAsync>d__.waitTimeout = waitTimeout;
			<WaitAsync>d__.<>1__state = -1;
			<WaitAsync>d__.<>t__builder.Start<ReadWriteNetHelper.<WaitAsync>d__7>(ref <WaitAsync>d__);
			return <WaitAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Int16,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C56 RID: 7254 RVA: 0x0008C4D0 File Offset: 0x0008A6D0
		[DebuggerStepThrough]
		public static Task<OperateResult<TimeSpan>> WaitAsync(IReadWriteNet readWriteNet, string address, short waitValue, int readInterval, int waitTimeout)
		{
			ReadWriteNetHelper.<WaitAsync>d__8 <WaitAsync>d__ = new ReadWriteNetHelper.<WaitAsync>d__8();
			<WaitAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
			<WaitAsync>d__.readWriteNet = readWriteNet;
			<WaitAsync>d__.address = address;
			<WaitAsync>d__.waitValue = waitValue;
			<WaitAsync>d__.readInterval = readInterval;
			<WaitAsync>d__.waitTimeout = waitTimeout;
			<WaitAsync>d__.<>1__state = -1;
			<WaitAsync>d__.<>t__builder.Start<ReadWriteNetHelper.<WaitAsync>d__8>(ref <WaitAsync>d__);
			return <WaitAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.UInt16,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C57 RID: 7255 RVA: 0x0008C534 File Offset: 0x0008A734
		[DebuggerStepThrough]
		public static Task<OperateResult<TimeSpan>> WaitAsync(IReadWriteNet readWriteNet, string address, ushort waitValue, int readInterval, int waitTimeout)
		{
			ReadWriteNetHelper.<WaitAsync>d__9 <WaitAsync>d__ = new ReadWriteNetHelper.<WaitAsync>d__9();
			<WaitAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
			<WaitAsync>d__.readWriteNet = readWriteNet;
			<WaitAsync>d__.address = address;
			<WaitAsync>d__.waitValue = waitValue;
			<WaitAsync>d__.readInterval = readInterval;
			<WaitAsync>d__.waitTimeout = waitTimeout;
			<WaitAsync>d__.<>1__state = -1;
			<WaitAsync>d__.<>t__builder.Start<ReadWriteNetHelper.<WaitAsync>d__9>(ref <WaitAsync>d__);
			return <WaitAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Int32,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C58 RID: 7256 RVA: 0x0008C598 File Offset: 0x0008A798
		[DebuggerStepThrough]
		public static Task<OperateResult<TimeSpan>> WaitAsync(IReadWriteNet readWriteNet, string address, int waitValue, int readInterval, int waitTimeout)
		{
			ReadWriteNetHelper.<WaitAsync>d__10 <WaitAsync>d__ = new ReadWriteNetHelper.<WaitAsync>d__10();
			<WaitAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
			<WaitAsync>d__.readWriteNet = readWriteNet;
			<WaitAsync>d__.address = address;
			<WaitAsync>d__.waitValue = waitValue;
			<WaitAsync>d__.readInterval = readInterval;
			<WaitAsync>d__.waitTimeout = waitTimeout;
			<WaitAsync>d__.<>1__state = -1;
			<WaitAsync>d__.<>t__builder.Start<ReadWriteNetHelper.<WaitAsync>d__10>(ref <WaitAsync>d__);
			return <WaitAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.UInt32,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C59 RID: 7257 RVA: 0x0008C5FC File Offset: 0x0008A7FC
		[DebuggerStepThrough]
		public static Task<OperateResult<TimeSpan>> WaitAsync(IReadWriteNet readWriteNet, string address, uint waitValue, int readInterval, int waitTimeout)
		{
			ReadWriteNetHelper.<WaitAsync>d__11 <WaitAsync>d__ = new ReadWriteNetHelper.<WaitAsync>d__11();
			<WaitAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
			<WaitAsync>d__.readWriteNet = readWriteNet;
			<WaitAsync>d__.address = address;
			<WaitAsync>d__.waitValue = waitValue;
			<WaitAsync>d__.readInterval = readInterval;
			<WaitAsync>d__.waitTimeout = waitTimeout;
			<WaitAsync>d__.<>1__state = -1;
			<WaitAsync>d__.<>t__builder.Start<ReadWriteNetHelper.<WaitAsync>d__11>(ref <WaitAsync>d__);
			return <WaitAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.Int64,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C5A RID: 7258 RVA: 0x0008C660 File Offset: 0x0008A860
		[DebuggerStepThrough]
		public static Task<OperateResult<TimeSpan>> WaitAsync(IReadWriteNet readWriteNet, string address, long waitValue, int readInterval, int waitTimeout)
		{
			ReadWriteNetHelper.<WaitAsync>d__12 <WaitAsync>d__ = new ReadWriteNetHelper.<WaitAsync>d__12();
			<WaitAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
			<WaitAsync>d__.readWriteNet = readWriteNet;
			<WaitAsync>d__.address = address;
			<WaitAsync>d__.waitValue = waitValue;
			<WaitAsync>d__.readInterval = readInterval;
			<WaitAsync>d__.waitTimeout = waitTimeout;
			<WaitAsync>d__.<>1__state = -1;
			<WaitAsync>d__.<>t__builder.Start<ReadWriteNetHelper.<WaitAsync>d__12>(ref <WaitAsync>d__);
			return <WaitAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Wait(System.String,System.UInt64,System.Int32,System.Int32)" />
		/// <param name="readWriteNet">通信对象</param>
		// Token: 0x06001C5B RID: 7259 RVA: 0x0008C6C4 File Offset: 0x0008A8C4
		[DebuggerStepThrough]
		public static Task<OperateResult<TimeSpan>> WaitAsync(IReadWriteNet readWriteNet, string address, ulong waitValue, int readInterval, int waitTimeout)
		{
			ReadWriteNetHelper.<WaitAsync>d__13 <WaitAsync>d__ = new ReadWriteNetHelper.<WaitAsync>d__13();
			<WaitAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<TimeSpan>>.Create();
			<WaitAsync>d__.readWriteNet = readWriteNet;
			<WaitAsync>d__.address = address;
			<WaitAsync>d__.waitValue = waitValue;
			<WaitAsync>d__.readInterval = readInterval;
			<WaitAsync>d__.waitTimeout = waitTimeout;
			<WaitAsync>d__.<>1__state = -1;
			<WaitAsync>d__.<>t__builder.Start<ReadWriteNetHelper.<WaitAsync>d__13>(ref <WaitAsync>d__);
			return <WaitAsync>d__.<>t__builder.Task;
		}
	}
}
